//
//  HYFStatusOriginalView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/24.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit
import SnapKit


class HYFStatusOriginalView: UIView {
    
    //记录当前原创微博的底部约束
    var originalViewBottomConstraints:Constraint?
    
    //定义一个 viewModel 供外界调用
    var statusViewModel:HYFStatusViewModel?{
        
        didSet{
            
         //设置头像
            headImageView.hyf_setImage(statusViewModel?.status?.user?.profile_image_url, placeholderImgName: "avatar_default")
            
         //昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            
         //MARK: 时间和来源后续处理
 
           timeLabel.text =  statusViewModel?.sinaTimeStr
            
           sourceLabel.attributedText = statusViewModel?.sourceAttr
            
         //微博会员等级
            memberImgView.image = statusViewModel?.memberImage
            
         //微博认证用户
            avatarImgView.image = statusViewModel?.avatarImage
            
            //contentLabel.text = statusViewModel?.status?.text
            //dealContentText(statusViewModel?.status?.text)
            
            contentLabel.attributedText = statusViewModel?.originalContentAttr
            
            //卸载掉当前的约束
            self.originalViewBottomConstraints?.uninstall()
            
            //判断有没有配图
            if statusViewModel?.status?.picture_urls?.count > 0 {
                
                //有配图的情况下,先添加约束,在赋值
                pictureView.pictureInfo = statusViewModel?.status?.picture_urls
                
                //有配图
                self.snp_makeConstraints(closure: { (make) in
                    
                    self.originalViewBottomConstraints = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                //显示pictureView
                pictureView.hidden = false
            }else{
                
             //没有配图
                self.snp_makeConstraints(closure: { (make) in
                    
                    self.originalViewBottomConstraints = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                //隐藏 pictureView
                pictureView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
      setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
      backgroundColor = UIColor.whiteColor()
        
        //添加控件
      addSubview(headImageView)
      addSubview(nameLabel)
      addSubview(memberImgView)
      addSubview(timeLabel)
      addSubview(sourceLabel)
      addSubview(avatarImgView)
      addSubview(contentLabel)
      addSubview(pictureView)
        
        //设置约束
      headImageView.snp_makeConstraints { (make) in
        
        make.leading.top.equalTo(cellMargin)
        make.size.equalTo(CGSize(width: 35, height: 35))
        
        }
        
      nameLabel.snp_makeConstraints { (make) in
        
        make.leading.equalTo(headImageView.snp_trailing).offset(cellMargin)
        make.top.equalTo(headImageView)
        }
        
       memberImgView.snp_makeConstraints { (make) in
        
        make.leading.equalTo(nameLabel.snp_trailing).offset(cellMargin)
        make.centerY.equalTo(nameLabel)
        
        }
        
        timeLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(cellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        avatarImgView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(headImageView.snp_bottom).offset(cellMargin)
            make.leading.equalTo(headImageView)
        }
        
        //给当前的原创微博设置约束
        self.snp_makeConstraints { (make) in
            
           self.originalViewBottomConstraints =  make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
        }
        
        pictureView.snp_makeConstraints { (make) in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.leading.equalTo((SCREENW - (cellWH * 3 + cellMargin * 2)) / 2)
        }
    }
    
    //懒加载控件
    private lazy var headImageView:UIImageView = UIImageView(imgName: "avatar_default")
    private lazy var nameLabel:UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15)
    private lazy var memberImgView:UIImageView = UIImageView(imgName: "common_icon_membership")
    private lazy var timeLabel:UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    private lazy var sourceLabel:UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    private lazy var avatarImgView:UIImageView = UIImageView(imgName: "avatar_vgirl")
    
    private lazy var pictureView:HYFStatusPictureView = HYFStatusPictureView()
    
    //微博内容
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: SCREENW - 20)
}
